package com.xiyu.service.repository.elder.register;

import com.xiyu.service.model.bed.bed.BedInfoFetcher;
import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.elder.register.*;
import com.xiyu.service.model.fee.category.FeeCategoryFetcher;
import com.xiyu.service.model.nurse.plan.NursePlanDetailFetcher;
import com.xiyu.service.model.nurse.plan.NursePlanFetcher;
import com.xiyu.service.model.restaurant.recipe.RestaurantComboFetcher;
import com.xiyu.service.vo.elder.checkIn.CheckInQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

public interface ElderCheckInRepository extends JRepository<ElderCheckIn, Long> {
    ElderCheckInTable elderCheckInTable = ElderCheckInTable.$;

    default Page<ElderCheckIn> pageQuery(CheckInQueryInput inputVO){
        return sql().createQuery(elderCheckInTable)
                .whereIf(StringUtils.hasText(inputVO.getCheckInNo()), () ->  elderCheckInTable.checkInNo().like(inputVO.getCheckInNo()))
                .whereIf(StringUtils.hasText(inputVO.getElderName()), () -> elderCheckInTable.elder().name().like(inputVO.getElderName()))
                .whereIf(inputVO.getStatus() != null, elderCheckInTable.status().eq(inputVO.getStatus()))
                .orderBy(elderCheckInTable.id().desc())
                .select(elderCheckInTable.fetch(ElderCheckInFetcher.$.allTableFields()
                        .elder(ElderInfoFetcher.$.allTableFields())
                        .bedInfo(BedInfoFetcher.$.allTableFields())
                        .restaurantCombo(RestaurantComboFetcher.$.allTableFields())
                        .nursePlan(NursePlanFetcher.$.allTableFields())

                ))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<ElderCheckIn> findDetailById(Long id){
        return sql().createQuery(elderCheckInTable)
                .where(elderCheckInTable.id().eq(id))
                .select(elderCheckInTable.fetch(ElderCheckInFetcher.$.allTableFields()
                                .elder(ElderInfoFetcher.$.allTableFields())
                                .bedInfo(BedInfoFetcher.$.allTableFields())
                                .restaurantCombo(RestaurantComboFetcher.$.allTableFields())
                                .nursePlan(NursePlanFetcher.$.allTableFields()
                                        .details(NursePlanDetailFetcher.$.allTableFields()))
                                .fees(ElderCheckInFeeFetcher.$.allTableFields()
                                        .feeCategory(FeeCategoryFetcher.$.allTableFields()))
                        ))
                .fetchOptional();
    }

    Optional<ElderCheckIn> findByElderIdAndStatus(Long elderId, Integer status);

    default List<ElderCheckIn> findDetailByStatus(Integer status){
        return sql().createQuery(elderCheckInTable)
                .where(elderCheckInTable.status().eq(status))
                .select(elderCheckInTable.fetch(ElderCheckInFetcher.$.allTableFields()
                        .elder(ElderInfoFetcher.$.allTableFields())
                        .bedInfo(BedInfoFetcher.$.allTableFields())
                        .restaurantCombo(RestaurantComboFetcher.$.allTableFields())
                        .nursePlan(NursePlanFetcher.$.allTableFields()))
                )
                .execute();
    }

}